package com.yj.inventory.controller;

import com.yj.common.responses.TerminalResponse;
import com.yj.config.mybatis.pagination.Page;
import com.yj.inventory.dto.InventoryListDto;
import com.yj.inventory.dto.InventoryUpdateDto;
import com.yj.inventory.entity.Inventory;
import com.yj.inventory.entity.InventoryHistory;
import com.yj.inventory.service.InventoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Copyright 2017 (C) Yunjian-VC
 * Created on : 2017/12/12
 * Author     : Unbelievable9
 **/
@Api(tags = "易煤可视化-库存信息")
@Controller
@RequestMapping("/inventory")
public class InventoryController {

    @Autowired
    InventoryService inventoryService;

    @ApiOperation("新建库存记录")
    @PostMapping("/create")
    @ResponseBody
    public TerminalResponse<Object> create(@RequestHeader String token,
                                           @RequestBody Inventory inventory) {
        TerminalResponse<Object> terminalResponse;

        try {
            if (inventoryService.userValidation(token)) {
                inventoryService.createInventory(inventory, token);

                terminalResponse = new TerminalResponse<>(true, "新建库存记录成功", null);
            } else {
                terminalResponse = new TerminalResponse<>(false, "用户不是运营人员，无法新建库存", null);
            }
        } catch (Exception e) {
            e.printStackTrace();

            terminalResponse = new TerminalResponse<>(false, "新建库存记录失败", null);
        }

        return terminalResponse;
    }

    @ApiOperation("更新库存记录")
    @PostMapping("/update")
    @ResponseBody
    public TerminalResponse<Object> update(@RequestHeader String token,
                                           @RequestBody @Validated InventoryUpdateDto inventoryUpdateDto) {
        TerminalResponse<Object> terminalResponse;

        try {
            if (inventoryService.userValidation(token)) {
                inventoryService.updateInventory(inventoryUpdateDto, token);

                terminalResponse = new TerminalResponse<>(true, "更新库存记录成功", null);
            } else {
                terminalResponse = new TerminalResponse<>(false, "用户不是运营人员，无法更新库存", null);
            }
        } catch (Exception e) {
            e.printStackTrace();

            terminalResponse = new TerminalResponse<>(false, "更新库存记录失败", null);
        }

        return terminalResponse;
    }

    @ApiOperation("查看记录")
    @GetMapping("/check-record")
    @ResponseBody
    public TerminalResponse<Object> checkRecord(@RequestHeader String token,
                                                @ApiParam("库存ID") @RequestParam Integer id) {
        TerminalResponse<Object> terminalResponse;

        try {
            if (inventoryService.userValidation(token)) {
                List<InventoryHistory> inventoryHistoryList = inventoryService.getInventoryHistoryByID(id);

                terminalResponse = new TerminalResponse<>(true, "获取库存更新记录成功", inventoryHistoryList);
            } else {
                terminalResponse = new TerminalResponse<>(false, "用户不是运营人员， 无法查看库存更新记录", null);
            }
        } catch (Exception e) {
            e.printStackTrace();

            terminalResponse = new TerminalResponse<>(false, "获取库存更新记录失败", null);
        }

        return terminalResponse;
    }

    @ApiOperation("获取库存列表信息")
    @GetMapping("/list")
    @ResponseBody
    public TerminalResponse<Object> list(@RequestHeader String token,
                                         @ModelAttribute @Validated InventoryListDto inventoryListDto) {
        TerminalResponse<Object> terminalResponse;

        try {
            if (inventoryService.userValidation(token)) {
                Page<Inventory> inventoryPage = inventoryService.getInventoryList(inventoryListDto, token);

                terminalResponse = new TerminalResponse<>(true, "获取库存列表成功", inventoryPage);
            } else {
                terminalResponse = new TerminalResponse<>(false, "用户不是运营人员， 无法获取库存列表", null);
            }
        } catch (Exception e) {
            e.printStackTrace();

            terminalResponse = new TerminalResponse<>(false, "获取库存列表失败", null);
        }

        return terminalResponse;
    }
}
